package com.zjc.yohoshop.mvp.view.fragment;

import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Toast;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.youth.banner.Banner;
import com.youth.banner.BannerConfig;
import com.youth.banner.listener.OnBannerClickListener;
import com.zjc.lib_core.base.BaseFragment;
import com.zjc.yohoshop.R;
import com.zjc.yohoshop.bean.ShowEntity;
import com.zjc.yohoshop.di.compont.DaggerShowFragmentCompont;
import com.zjc.yohoshop.di.modules.ShowFragmentModules;
import com.zjc.yohoshop.listener.HomeBannerImageLoader;
import com.zjc.yohoshop.mvp.contract.ShowFragmentContract;
import com.zjc.yohoshop.mvp.persenter.ShowFragmentPresenter;
import com.zjc.yohoshop.mvp.view.adapter.ShowAdapter;
import com.zjc.yohoshop.network.API;

import java.util.ArrayList;
import java.util.List;

/**
 * 逛下的Show
 */
public class ShowFragment extends BaseFragment<ShowFragmentPresenter> implements ShowFragmentContract.ShowFragmentIView, BaseRefreshListener {
    private boolean banner_0 = true;
    private RecyclerView rv_show;
    private PullToRefreshLayout ptl_show;
    private Banner banner;
    private List<ShowEntity.BannerBean> bannerBeans;
    private ShowAdapter adapter;

    @Override
    public View createView(LayoutInflater inflater) {
        View view = inflater.inflate(R.layout.fragment_show, null);
        return view;
    }

    @Override
    public void init(View view) {
        ptl_show = view.findViewById(R.id.ptl_show);

        //获取头布局
        View view_head = View.inflate(getContext(), R.layout.item_show_banner, null);
        banner = view_head.findViewById(R.id.ban_item_show_head);
        rv_show = view.findViewById(R.id.rv_show);
        //recyclerview配置
        rv_show.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new ShowAdapter(R.layout.item_show);
        adapter.addHeaderView(view_head, 0);
        rv_show.setAdapter(adapter);
    }

    @Override
    public void initData() {
        p.request();
    }

    @Override
    public void setupDagger() {
        DaggerShowFragmentCompont.builder().showFragmentModules(new ShowFragmentModules(this))
                .build().inject(this);
    }

    @Override
    public void initListener() {
        ptl_show.setRefreshListener(this);
        //数据点击事件
        adapter.setOnItemClickListener(new BaseQuickAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(BaseQuickAdapter adapter, View view, int position) {
                Toast.makeText(getActivity(), ((ShowAdapter)adapter).getData().get(position).goods_name, Toast.LENGTH_SHORT).show();
            }
        });
        //banner点击事件
        banner.setOnBannerClickListener(new OnBannerClickListener() {
            @Override
            public void OnBannerClick(int position) {
                Toast.makeText(getActivity(), bannerBeans.get(position).recommend_name, Toast.LENGTH_SHORT).show();
            }
        });

    }

    @Override
    public void success(ShowEntity entity) {
        if (entity.values == null || entity.values.size() == 0){
            Toast.makeText(getActivity(), "没数据了", Toast.LENGTH_SHORT).show();
            return;
        }

        adapter.addData(entity.values);

        if (banner_0) {
            bannerBeans = entity.banner;
            List<String> strs = new ArrayList<>();
            for (ShowEntity.BannerBean bannerBean : entity.banner) {
                strs.add(API.BaseUrl + bannerBean.recommend_url);
            }
            banner.setImages(strs);
            banner.setImageLoader(new HomeBannerImageLoader());
            banner.isAutoPlay(true);
            banner.setIndicatorGravity(BannerConfig.CENTER);
            banner.setDelayTime(1 * 2000);
            banner.start();
            banner_0 = !banner_0;
        }
    }

    @Override
    public void showDialog() {

    }

    @Override
    public void hideDialog() {

    }


    @Override
    public void refresh() {
        adapter.getData().clear();
        p.refresh();
        ptl_show.finishRefresh();
    }

    @Override
    public void loadMore() {
        p.loadMore();
        ptl_show.finishLoadMore();
    }
}
